package com.sgb.kjwl.viewmodel.self;

import androidx.databinding.ObservableField;
import android.widget.Toast;

import com.sgb.kjwl.AppConstant;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.user.AuthPhotoEntity;
import com.sgb.kjwl.model.entity.user.SupplierAuthEntity;
import com.sgb.kjwl.repository.SelfRepertory;
import com.sgb.kjwl.view.ui.activity.self.authentication.SupplierAuthActivity;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MLog;
import com.swgk.core.util.MToast;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import io.reactivex.functions.Consumer;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class SupplierAuthModel extends BaseViewModel {
    public ObservableField<SupplierAuthEntity> supplierAuthEntity = new ObservableField<>(new SupplierAuthEntity());
    private SupplierAuthActivity activity;
    private SelfRepertory repertory;

    public SupplierAuthModel(SupplierAuthActivity activity, SelfRepertory repertory) {
        this.activity = activity;
        this.repertory = repertory;
    }

    //上传营业执照
    public void upLicenseModel(File file) {
        DialogHelper.showProgressDialog(activity, null, "文件上传中...", 0, false, null).setCanceledOnTouchOutside(true);
        Map<String, RequestBody> photos = new HashMap<>();
        RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        photos.put("file" + "\"; filename=\"" + "image.jpg\"", requestBody);

        APIManager.getInstance().getUploadAPI().uploadPhoto(photos).enqueue(new Callback<BaseEntity<AuthPhotoEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<AuthPhotoEntity>> call, Response<BaseEntity<AuthPhotoEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() != null && response.body().getState().equals("ok")) {
                    if (response.body().getData() != null) {
                        Toast.makeText(activity, "上传成功!", Toast.LENGTH_SHORT).show();
                        supplierAuthEntity.get().setLicenseUrl(response.body().getData().getImageUrl());
                        supplierAuthEntity.get().setBusiness(response.body().getData().getBusiness());
                        supplierAuthEntity.get().setRegisteredCapital(response.body().getData().getCapital());
                        supplierAuthEntity.get().setEstablishmentDate(response.body().getData().getEstablish_date());
                        supplierAuthEntity.get().setCompanyName(response.body().getData().getName());
                        supplierAuthEntity.get().setLegalPerson(response.body().getData().getPerson());
                        supplierAuthEntity.get().setCreditCode(response.body().getData().getReg_num());
                        supplierAuthEntity.get().setCompanyType(response.body().getData().getType());
                    } else {
                        activity.removeList();
                        Toast.makeText(activity, "营业执照识别失败,请重新上传!", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    activity.removeList();
                    MToast.showToast(activity, "营业执照识别失败,请重新上传!");
                }

            }

            @Override
            public void onFailure(Call<BaseEntity<AuthPhotoEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(activity, "营业执照识别失败,请重新上传!");
            }
        });

//        DialogHelper.showProgressDialog(activity, null, "文件上传中...", 0, false, null).setCanceledOnTouchOutside(false);
//        repertory.uploadPhoto(file)
//                .subscribe(new Consumer<BaseEntity<PhotoEntity>>() {
//                    @Override
//                    public void accept(BaseEntity<PhotoEntity> photoEntityBaseEntity) throws Exception {
//                        DialogHelper.dismissProgressDialog();
//                        if (photoEntityBaseEntity.getData() != null) {
//                            Gson gson = new Gson();
//                            CertificateEntity certificateEntity = gson.fromJson(photoEntityBaseEntity.getData().getCertificateJson(), CertificateEntity.class);
//                            if (certificateEntity != null) {
//                                Toast.makeText(activity, "上传成功!", Toast.LENGTH_SHORT).show();
//                                supplierAuthEntity.get().setLicenseUrl(photoEntityBaseEntity.getData().getKey());
//                                supplierAuthEntity.get().setCompanyAddress(certificateEntity.getAddress());
//                                supplierAuthEntity.get().setBusiness(certificateEntity.getBusiness());
//                                supplierAuthEntity.get().setRegisteredCapital(certificateEntity.getCapital());
//                                supplierAuthEntity.get().setEstablishmentDate(certificateEntity.getEstablishDate());
//                                supplierAuthEntity.get().setCompanyName(certificateEntity.getCompanyName());
//                                supplierAuthEntity.get().setLegalPerson(certificateEntity.getLegalPerson());
//                                supplierAuthEntity.get().setCreditCode(certificateEntity.getCreditCode());
//                                supplierAuthEntity.get().setCompanyType(certificateEntity.getCompanyType());
//                            } else {
//                                activity.removeList();
//                                Toast.makeText(activity, "营业执照识别失败,请重新上传!", Toast.LENGTH_SHORT).show();
//                            }
//                        } else {
//                            activity.removeList();
//                            Toast.makeText(activity, "营业执照识别失败,请重新上传!", Toast.LENGTH_SHORT).show();
//                        }
//                    }
//                }, new Consumer<Throwable>() {
//                    @Override
//                    public void accept(Throwable throwable) throws Exception {
//                        DialogHelper.dismissProgressDialog();
//                        Toast.makeText(activity, "接口错误：" + throwable.getMessage(), Toast.LENGTH_SHORT).show();
//                        MLog.d(AppConstant.API_LOG, "error " + throwable.getMessage());
//                    }
//                });
    }

    //上传授权委托书
    public void upEntrustModel(File file) {
        DialogHelper.showProgressDialog(activity, null, "文件上传中...", 0, false, null).setCanceledOnTouchOutside(false);
        repertory.uploadFile(file)
                .subscribe(new Consumer<BaseEntity>() {
                    @Override
                    public void accept(BaseEntity baseEntity) throws Exception {
                        DialogHelper.dismissProgressDialog();
                        if (baseEntity.getData() != null) {
                            Toast.makeText(activity, "上传成功!", Toast.LENGTH_SHORT).show();
                            supplierAuthEntity.get().setAuthorization(baseEntity.getData().toString());
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        DialogHelper.dismissProgressDialog();
                        Toast.makeText(activity, "接口错误：" + throwable.getMessage(), Toast.LENGTH_SHORT).show();
                        MLog.d(AppConstant.API_LOG, "error " + throwable.getMessage());
                    }
                });
    }
}
